package com.lyn.dwjhb.convertor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;

import com.lyn.dwjhb.model.ReportDlscqk;


public class ReportDlscqkConvertor extends BaseConvertor{
	public ReportDlscqkConvertor() {
		super();
	}
	
	public ReportDlscqkConvertor(QueryRunner rq) {
		super(rq);
	}

	private String getInsertSql(ReportDlscqk reportDlscqk) {
		String isRequired = reportDlscqk.getIsRequired()==null?"0":"1";
		
		String sql = "insert into report_dlscqk (guid, gmt_create, gmt_modified, sorting, "
				+ "dept_id, report_id, year, month, "
				+ "varify_flag, item_name, value, value_sum, "
				+ "value_past, value_past_sum, value_rate, value_rate_sum, "
				+ "unit, is_required) values("
				+ "sys_guid(), sysdate, sysdate, "+reportDlscqk.getSorting()+","
				+ wrapper(reportDlscqk.getDeptId())+","+ wrapper(reportDlscqk.getReportId())+","
				+ reportDlscqk.getYear() +"," + reportDlscqk.getMonth() + "," 
				+ wrapper(reportDlscqk.getVarifyFlag())+","+ wrapper(reportDlscqk.getItemName())+","
				+ reportDlscqk.getValue() +"," + reportDlscqk.getValueSum() + ","
				+ reportDlscqk.getValuePast() +"," + reportDlscqk.getValuePastSum() + ","
				+ reportDlscqk.getValueRate() +"," + reportDlscqk.getValueRateSum() + ","
				+ wrapper(reportDlscqk.getUnit())+","+ wrapper(isRequired)+");";
		return sql;
	}
	
	
	
	public void toFile(List<ReportDlscqk> list) {
		File file = new File("resource/PROC_INSERT_REPORT_DLSCQK.sql");
		if(!file.exists()) {
			file.delete();
		}
        try {
        	file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        try(
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        	BufferedWriter bw = new BufferedWriter(osw);) {
        	String sql = "create or replace procedure PROC_INSERT_REPORT_DLSCQK is";
        	this.writeLine(bw, sql);
        	sql = "begin";
        	this.writeLine(bw, sql);
        	for(ReportDlscqk reportDlscqk : list) {
        		sql = getInsertSql(reportDlscqk);
        		this.writeLine(bw, sql);
    		}
        	sql = "commit;";
        	this.writeLine(bw, sql);
        	sql = "end PROC_INSERT_REPORT_DLSCQK;";
        	this.writeLine(bw, sql);
        } catch (Exception e) {
            e.printStackTrace(); 
        } 
	}
	
	
	
}
